package com.zhongjian.technologycal.mall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.bean.OrdersDetailsBean;

import java.util.List;

/**
 * Created by wei on 2018/1/29.
 */

public class OrdersDetailsListAdapter extends BaseAdapter {

    private Context context;
    private List<OrdersDetailsBean> listItems;
    private LayoutInflater listContainer;

    public final class ViewHolder {
        public ImageView order_iv_shopping_photo;
        public TextView order_tv_shopping_name;
        public TextView order_tv_shopping_price;
        public TextView order_tv_shopping_num;

    }

    public OrdersDetailsListAdapter(Context context, List<OrdersDetailsBean> listItems) {
        this.context = context;
        listContainer = LayoutInflater.from(context);
        this.listItems = listItems;
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        OrdersDetailsListAdapter.ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new OrdersDetailsListAdapter.ViewHolder();
            convertView = listContainer.inflate(R.layout.items_order_details,
                    null);
            viewHolder.order_iv_shopping_photo = (ImageView) convertView
                    .findViewById(R.id.order_iv_shopping_photo);
            viewHolder.order_tv_shopping_name = (TextView) convertView
                    .findViewById(R.id.order_tv_shopping_name);
            viewHolder.order_tv_shopping_price = (TextView) convertView
                    .findViewById(R.id.order_tv_shopping_price);
            viewHolder.order_tv_shopping_num = (TextView) convertView
                    .findViewById(R.id.order_tv_shopping_num);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (OrdersDetailsListAdapter.ViewHolder) convertView.getTag();
        }
        Picasso.get().load(BaseContacts.BASE_URL+listItems.get(position).getData()
                .getOrderlines().get(position).getPhoto())
                .into(viewHolder.order_iv_shopping_photo);

        //将数据填充到具体的view中
        viewHolder.order_tv_shopping_name.setText("" + listItems.get(position).getData().getOrderlines().get(position).getProductname());

        viewHolder.order_tv_shopping_price.setText("" + listItems.get(position).getData().getOrderlines().get(position).getPrice());
        viewHolder.order_tv_shopping_num.setText("x" + listItems.get(position).getData().getOrderlines().get(position).getProductnum());



        return convertView;
    }
}
